package com.app.beans;


import com.app.model.Personnel;
import com.app.interfaces.AuthentificationService;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author Krachleur
 */

public class AuthentificationBean extends MessageBean{
    private AuthentificationService loginService;
    private String login;
    private String password;
    private String today;
    private Personnel pers;
    private String message;

    /** Creates a new instance of AuthentificationBean */
    public AuthentificationBean() {
    }

    public String getLogin() {
        return login;
    }

    public AuthentificationService getLoginService() {
        return loginService;
    }

    public String getMessage() {
        return message;
    }

    public String getPassword() {
        return password;
    }

    public Personnel getPers() {
        return pers;
    }

    public String getToday() throws Exception {
        Date maDateAvecFormat = new Date();
        SimpleDateFormat dateStandard = new SimpleDateFormat("EEEE dd MMMM yyyy");
        today = "" + dateStandard.format(maDateAvecFormat);
        dateStandard = null;
        return today;
    }

    public String connecter(){
        String droit = null;
        message = "";
        try{
            pers = loginService.findByLoginAndPassword(login, password);
            if(pers != null){
                if(pers.getPersDroit().equals("user")){
                    droit = "user";
                } else if(pers.getPersDroit().equals("admin")){
                    droit = "admin";
                }
                System.out.println("********Droit********" +droit );
                return droit;
            }  else {
                message = "Echec de connexion, verifier votre login et mot de pass !";
                style_message = "err_message";
                this.login = "";
                this.password = "";
                return "invalide";
            }
        } catch(Exception e){
            e.printStackTrace();
            message = "Echec de connexion, verifier votre login et mot de passe !";
            style_message = "err_message";
            this.login = "";
            this.password = "";
            return "invalide";
        }
    }

    public String deconnecter(){
        try{
            ExternalContext ExtContext = FacesContext.getCurrentInstance().getExternalContext();
            ExtContext.getSessionMap().clear();
        }catch(Exception e){
            e.printStackTrace();
        }
        return "ok";
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setLoginService(AuthentificationService loginService) {
        this.loginService = loginService;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setPers(Personnel pers) {
        this.pers = pers;
    }

    public void setToday(String today) {
        this.today = today;
    }



}
